/*
 * Copyright (c) 2012 Aleksander Zotov, Benedikt Markt, Aliaksei Babkovich
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 *  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 *  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package gui;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import user.UserManager;
import utils.InvalidInputException;

/**
 * Bean for managing logins
 * 
 * CANT BE TESTED WITH JUNIT
 * 
 * @author Aleksander Zotov
 * 
 */
@ManagedBean(name = "LoginBean")
public class LoginBean {

	private String name;
	private String password;
	private static boolean isLoggedIn = false;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public static boolean isLoggedIn() {
		return isLoggedIn;
	}

	/**
	 * Validates the input username and password and shows an appropriate error
	 * message if needed
	 * 
	 * @return "success" if a user with given username exists and the password
	 *         matches the user; "false" if there were errors in validating the
	 *         user
	 * @throws InvalidInputException
	 */
	public String validateUser() throws InvalidInputException {
		MessageController messageController = new MessageController();
		UserManager userManager = UserManager.getInstance();
		if (name.equals("")) {
			messageController.showErrorMessage("Benutzername fehlt");
			return "false";
		}
		if (password.equals("")) {
			messageController.showErrorMessage("Passwort fehlt");
			return "false";
		}
		if (!userManager.isExistingUser(name)) {
			messageController
					.showErrorMessage("Ihr Benutzername oder Ihr Passwort ist falsch");
			return "false";
		}
		if (!userManager.isCorrectLogin(name, password)) {
			messageController
					.showErrorMessage("Ihr Benutzername oder Ihr Passwort ist falsch");
			return "false";
		}
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest) context
				.getExternalContext().getRequest();
		HttpSession httpSession = request.getSession(false);
		httpSession.setAttribute("username", name);
		isLoggedIn = true;
		try {
			// Redirect user to main page
			FacesContext.getCurrentInstance().getExternalContext()
					.redirect("/SatPro/faces/main.xhtml");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * Logs the user out and redirects him to login page
	 * 
	 * @throws IOException
	 */
	public void logOut() throws IOException {
		RequestContext context = RequestContext.getCurrentInstance();
		isLoggedIn = false;
		FacesContext.getCurrentInstance().getExternalContext()
				.redirect("/SatPro/faces/login.xhtml");
	}
}
